package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.presenter.IPresenter;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.qcloud.core.auth.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import cn.sirun.com.fca.account.constant.AccountCommonConst;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;

import static com.tencent.cloud.iov.util.ResourcesUtils.getResources;

public class SystemSettingFlowView extends BaseLoadingFlowView {

    private TextView mLanguageTV, mNewVersionTV, mVersionTV;
    private LinearLayout mLanguageLL, mAboutLL;

    private OptionsPickerView mLanguageOPV;

    private String language;

    public SystemSettingFlowView(Activity activity) {
        super(activity);
    }

    public SystemSettingFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(
                activity, ResourcesUtils.getString(R.string.system_setting),
                ResourcesUtils.getDrawable(R.color.white));
    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    private void initView() {
        setContentView(R.layout.system_setting_activity);
        final Activity activity = getActivity();
        mLanguageTV = activity.findViewById(R.id.tv_language_value);
        mNewVersionTV = activity.findViewById(R.id.tv_new_verson_value);
        mVersionTV = activity.findViewById(R.id.tv_verson_value);
        mLanguageLL = activity.findViewById(R.id.ll_language);
        mAboutLL = activity.findViewById(R.id.ll_about);
        language = TXSharedPreferencesUtils.getValue("app_language");

        mLanguageLL.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                mLanguageOPV.show();
            }
        });

        mAboutLL.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String path = PersonalCenterActivityConst.Path.ABOUT_VERSION;
                Intent intent = Router.fromPath(path);
                ActivityUtils.startActivityForResult(getActivity(), intent);
            }
        });

        initLanguagePicker();
        if(!TextUtils.isEmpty(language)){
            mLanguageTV.setText(language);
        }
    }

    private void initLanguagePicker() {
        final List<String> testList = new ArrayList<>();
        testList.add("中文");
        testList.add("English");
        mLanguageOPV = new OptionsPickerBuilder(getActivity(), new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
//                String tx = testList.get(options1).getPickerViewText();
//                mNameTV.setText(tx);
                mLanguageTV.setText(testList.get(options1));
            }
        })
                .setLayoutRes(R.layout.setting_language_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvCancle = (TextView) v.findViewById(R.id.tv_cancle);
                        final TextView tvTitle = (TextView) v.findViewById(R.id.tv_title);
                        TextView tvComplete = (TextView) v.findViewById(R.id.tv_complete);
                        tvComplete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mLanguageOPV.returnData();
                                mLanguageOPV.dismiss();
                                if(mLanguageTV.getText().equals("中文")||mLanguageTV.getText().equals("Chinese")){
                                    TXSharedPreferencesUtils.setValue("app_language", "中文");
                                    changeAppLanguage(Locale.SIMPLIFIED_CHINESE);
                                }else if(mLanguageTV.getText().equals("English")||mLanguageTV.getText().equals("英语")){
                                    TXSharedPreferencesUtils.setValue("app_language", "English");
                                    changeAppLanguage(Locale.US);
                                }
                            }
                        });

                        tvCancle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mLanguageOPV.dismiss();
                            }
                        });
                    }
                })
                .isDialog(false)
                .setCyclic(false, false, false)
                .setDividerColor(R.color.grey_DD)
                .setLineSpacingMultiplier(2f)
                .build();

        mLanguageOPV.setPicker(testList);
    }

    /**
     * 更改应用语言
     *
     * @param locale
     */
    public void changeAppLanguage(Locale locale) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        Configuration configuration = getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        getResources().updateConfiguration(configuration, metrics);
        //重新启动Activity
        Intent intent = Router.fromPath(PersonalCenterActivityConst.Path.GO_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        ActivityUtils.startActivity(getActivity(),intent);
        getActivity().finish();
    }



}
